//所有按钮
var li = document.querySelectorAll('li');
//计算显示内容（包括运算符）
var com = document.querySelector('.com');
//显示输入的数字及结果
var show = document.querySelector('.show');
//接收计算内容
let comx='';
//接收显示内容
let showx=0;
//
let first,second;
//传递show的值
let sa='';
//运算符
let arr='';
//上一个运算符
let that='';


//触发点击事件
li.forEach(item=>{

	item.onclick=function(){
		//判断点击类别
		switch(item.className){
			//点击数字按钮
			// case 'num':
			// break;
			//点击运算符
			case 'ope':
				arr=item.innerText;
				if(!first){
					first=showx*1;
					showx=first;
					showa();
					sa='';
				}else{
					second=showx*1;
					showx=0;
					showa();
					sa='';
				}
				
				if(second){
					comput();
				}else{
					comx=first+item.innerText;
					that=item.innerText;
					coma();
				}
				
			break ;
			//点击清除
			case 'cle':
				comx=0;
				coma();
				showx=0;
				showa();
				first=''
				second=''
				sa=''
			break;
			//点击等于
			case 'val':
				if(that!=''){
					arr=item.innerText;
					comput();
				}

			break;
			
			default:

				//强制显示,只有一个点
				if(item.innerText!='.'  && item.innerText!='+/-'){
					sa+=item.innerText;	
				}
				shai(sa);
				
				
				if(item.innerText=='.' && sa.indexOf('.')==-1){
					sa+='.';
					showx+='.';
					showa();
				}
				if(sa=='' && com.innerText.charAt(length-1)=='='){
					comx='';
					coma();
				}
				//正负转换
				
				if(showx!=0){
					
					if( item.innerText=='+/-'  && show.innerText.charAt('0')!='-'){
						sa='-'+sa
					}else if(show.innerText.charAt('0')=='-'){
						sa=Math.abs(sa);
					}
					
				}
				
		
			break;
		}
		
	}
})

//计算显示内容
function coma(){
	com.innerText=comx;
}
//显示结果及其他
function showa(){
	show.innerText=showx;
}
//计算
function comput(){

	switch (arr){
		case '+':
			break;
		case '-':
			break;
		case 'x':
			break;
		case '/':
			break;
		
	}

		first=first*1;
		second=second*1
		switch (arr){
			case '+': 	
			
						if(arr!=that && that!='=' ){
							recom();
						}
						first=showx=first+second;
						showa()
						comx=showx+arr;
						coma();	
						that=arr;
				break;
			case '-':	
						console.log(first,second)
						if(arr!=that && that!='='){
							recom();
						}
						
						first=showx=first-second;
						console.log(first,second)
						showa()
						console.log(first,second)
						comx=showx+arr;
						coma();
						that=arr;
						
				break;
			case 'x':	
						if(arr!=that && that!='=' ){
							recom();
						}
						console.log(second)
						first=showx=first*second;
						console.log(first)
						showa()
						comx=showx+arr;
						coma();
						that=arr;
				break;
			case '/':
						if(second!='0'){
							if(arr!=that && that!='=' ){
							recom();
							}
						first=showx=first/second;
						
						showa();
						comx=showx+arr;
						coma();	
						that=arr;
						}else{
							showx='除数不能为0';
							showa();
							comx='';
							coma();
						}
		
				break;
			case '=':	
						if(that!='='){
							second=showx
							let lascom=first+that+second+'=';
							arr=that;
							comput();
							comx=lascom
							coma();
							that='='
							first=''
							second=''
							sa=''
						}

				break;

		}



	
}

//筛选
function shai(e){
	let cc;
	e = typeof e == 'number' ? e.toString() : e;
		if(e.charAt(0)=='.' && e=='.'){
			cc=0+e;
		}else if(e.charAt(length-1)==0 && e.indexOf('.')!=-1){
			cc=e;
		}else if(1){
			cc=e*1+'';
		}
		 showx=cc;
	
	showa();
	
	coma();
	
	
}

//重复计算
function recom(){
	let thatarr=arr;
	arr=that;
	comput();
	arr=thatarr;
	if(arr=='+' || arr=='-'){
		second=0;
	}else{
		second=1;
	}
}

